﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils;
using Settings;
using System.Data;
using System.Data.SqlClient;

namespace PanasonicWarranty.Business
{
    public partial class SysGroupRoleMenu : BaseClass
    {
        public static SysGroupRoleMenu GetByGroupIDAndRoleIDAndMenuID(int GroupID, int RoleID, int MenuID)
        {
            try
            {
                return ObjectClass.CreateInstance<SysGroupRoleMenu>
                (
                    SqlHelper.ExecuteReader(AppSetting.ConnectionString, CommandType.Text, string.Format("SELECT * FROM SysGroupRoleMenu WHERE GroupID = {0} AND RoleID = {1} AND MenuID = {2}", GroupID, RoleID, MenuID))
                );
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static bool Delete1(int GroupID, int RoleID, int MenuID)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(AppSetting.ConnectionString))
                {
                    int result = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, string.Format("DELETE FROM SysGroupRoleMenu WHERE GroupID = {0} AND RoleID = {1} AND MenuID = {2}", GroupID, RoleID, MenuID));
                    return (result > 0);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Hiện tại các hàm gen ra chưa sử dụng được do chưa có store được gen trong csdl
        /// </summary>
        /// <returns></returns>
        public bool Save1()
        {
            if (GetByGroupIDAndRoleIDAndMenuID(GroupID, RoleID, MenuID) == null)
            {
                try
                {
                    using (SqlConnection conn = new SqlConnection(AppSetting.ConnectionString))
                    {
                        int result = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, string.Format("INSERT INTO SysGroupRoleMenu (GroupID, RoleID, MenuID) VALUES ({0},{1},{2})", GroupID, RoleID, MenuID));
                        return (result > 0);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else return false;
        }

        public static bool CheckExists(int groupId, int menuId, string roleCode)
        {
            try
            {
                return (bool)ExecuteScalar("usp_SysGroupRoleMenu_CheckExists", CommandType.StoredProcedure,
                        new SqlParameter("@GroupID", groupId),
                        new SqlParameter("@MenuID", menuId),
                        new SqlParameter("@RoleCode", roleCode));
            }
            catch
            {
                return false;
            }
        }

        public static void SetRight(int groupId, int menuId, string roleCode, bool check)
        {
            try
            {
                ExecuteProc("usp_SysGroupRoleMenu_SetRight",
                    new SqlParameter("@GroupID", groupId),
                    new SqlParameter("@MenuID", menuId),
                    new SqlParameter("@RoleCode", roleCode),
                    new SqlParameter("@Checked", check));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
